package new_clue

import (
	"hft/Http/Models/request"
	"hft/Http/Services/new_clue"
	"hft/Http/Services/new_clue/clue_model"
	utils "hft/Http/Utils"
	"hft/e"
	"strconv"

	"github.com/gin-gonic/gin"
)

type ActivityClue struct{}

// 留活动线索
func (*ActivityClue) Add(c *gin.Context) {
	city, _ := strconv.Atoi(c.Request.Header.Get("sid"))
	port, _ := strconv.Atoi(c.Request.Header.Get("port"))
	var data *clue_model.ActivityClue
	c.ShouldBindJSON(&data)
	ip := c.ClientIP()
	token := c.Request.Header.Get("user-token")
	if res, err := new_clue.ActivityClueService.Add(data, token, city, ip, port); err != nil {
		utils.FailWithMessage(err.Error(), c)
	} else {
		utils.OkWithData(res, c)
	}
}

// 验证
func (*ActivityClue) Valid(c *gin.Context) {
	city, _ := strconv.Atoi(c.Request.Header.Get("sid"))
	if city == 0 {
		utils.FailWithMessage(e.RegionNotFound, c)
		return
	}
	var data *clue_model.PhoneReq
	c.ShouldBind(&data)
	if token, err := new_clue.ValidUser(data, city); err != nil {
		utils.FailWithMessage(err.Error(), c)
	} else {
		utils.OkWithData(gin.H{"user-token": token}, c)
	}
}

func (*ActivityClue) Send(c *gin.Context) {
	var phone *request.SendCode

	c.ShouldBindJSON(&phone)

	new_clue.SendValid(phone.Phone)

	utils.Ok(c)
}

// 微聊登录
func (*ActivityClue) ChatLogin(c *gin.Context) {
	var body *request.ChatLogin
	c.ShouldBindJSON(&body)
	sid, _ := strconv.Atoi(c.Request.Header.Get("sid"))
	port, _ := strconv.Atoi(c.Request.Header.Get("port"))
	ip := c.ClientIP()
	token, err := new_clue.ActivityClueService.ChatLogin(ip, port, body.Phone, body.LouID, body.Code, sid)
	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}
	utils.OkWithData(gin.H{"user-token": token}, c)
}

var ClueController = new(ActivityClue)
